﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class tcpipControl : UserControl
    {
        //private StreamWriter logWriter;
        public delegate void SendDataHandler(string data);
        public event SendDataHandler OnSendData;

        public tcpipControl()
        {
            InitializeComponent();
            
        }

        private void tcpipControl_Load(object sender, EventArgs e)
        {
            
        }
       
        public void Log(string message)
        {
            //logWriter = new StreamWriter("log");
            try
            {
                string logMessage = $"{DateTime.Now}: {message}";
                if (IsHandleCreated)
                {
                    Invoke((Action)(() =>
                    {
                        if (tbRecieve != null)
                        {
                            tbRecieve.AppendText(logMessage + Environment.NewLine);
                        }
                    }));
                }
                else
                {
                    // Handle the case where the handle is not created, e.g., by queuing the log message
                    Console.WriteLine("Handle not created, log message: " + logMessage);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error during logging: {ex.Message}");
            }
        }

        private void butSendData_Click(object sender, EventArgs e)
        {
            string data = tbSend.Text.Trim();
            OnSendData?.Invoke(data);
        }
    }
        
  
}
